<template>
  <div>
    <el-dialog :title="info.isAdd ?'商品添加':'商品修改'" :visible.sync="info.isshow" @close="close">
      user:{{user}}
      attrsArr:{{attrsArr}}
      <el-form :model="user">
        <el-form-item label="一级分类" label-width="120px">
          <el-select v-model="user.first_cateid" @change="change_first_cate">
            <el-option v-for="item in cateList" :key="item.id" :label="item.catename" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级分类" label-width="120px">
          <el-select v-model="user.second_cateid">
            <el-option v-for="item in second_cate_list" :key="item.id" :label="item.catename" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品名称" label-width="120px">
          <el-input v-model="user.goodsname"></el-input>
        </el-form-item>
        <el-form-item label="价格" label-width="120px">
          <el-input v-model="user.price"></el-input>
        </el-form-item>
        <el-form-item label="市场价格" label-width="120px">
          <el-input v-model="user.market_price"></el-input>
        </el-form-item>
        <el-form-item label="图片" label-width="120px">
          <upload-img @sendFile="user.img=$event" ref="upload"></upload-img>
        </el-form-item>
        <el-form-item label="商品规格" label-width="120px">
          <el-select v-model="user.specsid">
            <el-option label="家用电器"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="规格属性" label-width="120px">
          <el-select v-model="user.specsattr" multiple>
            <el-option label="家用电器"></el-option>
          </el-select>
        </el-form-item>
                <el-form-item label="是否新品" label-width="120px">
          <el-radio label="1">是</el-radio>     
          <el-radio label="2">否</el-radio>
        </el-form-item>
                        <el-form-item label="是否热卖" label-width="120px">
          <el-radio label="1">是</el-radio>     
          <el-radio label="2">否</el-radio>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch v-model="user.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
        <el-form-item label="描述" label-width="120px">
          <textarea cols="30" rows="10"></textarea>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="add()">添加</el-button>
        <el-button type="primary" v-else @click="update">修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { IndexRoutes } from "../../../router/index";
import { successAlert, errorAlert } from "../../../utils/alert.js";
import { reqspecsinfo, reqspecsedit, reqspecsadd, reqcatelist } from "../../../http/api";
import path from "path";
import { reqcateadd } from "../../../http/api";
import { formClose } from "../../../mixins";

export default {
  //接受info
  props: ["info"],
  mixins: [formClose],
  data() {
    return {
      //初始化表单数据
      user: {
         first_cateid:"",
         second_cateid:"",
         goodsname:"",
         price:"",
         market_price:"",
         img:"",
         description:"",
         specsid:"",
         specsattr:"",
         isnew:"",
         ishot:"",
         status:1
      },
      //二级分类列表
      second_cate_list:[],
      attrsArr: [
        {
          val: ""
        },
        {
          val: ""
        }
      ]
    };
  },
  computed: {
    ...mapGetters({
      //取出一级分类的页表
      cateList:"cate/arr"
    })
  },
  mounted() {
    this.reqCatArr()
  },
  methods: {
    ...mapActions({
      changeUserInfo: "changeUserInfo",
      reqCatArr: "cate/reqArr"
    }),
    //选了一级分类
    change_first_cate(){
      //清空二级分类选中的值
      this.user.second_cateid=""
     //根据一级分类取出二级分类的列表
     reqcatelist({pid:this.user.first_cateid}).then(res=>{
       console.log(res.data.list);
        if(res.data.code==200){
          this.second_cate_list=res.data.list
          // console.log(this.second_cate_list);
        }
     })
    },

    //清空user
    empty() {
      this.user = {
         first_cateid:"",
         second_cateid:"",
         goodsname:"",
         price:"",
         market_price:"",
         img:"",
         description:"",
         specsid:"",
         specsattr:"",
         isnew:"",
         ishot:"",
         status:""
      };
      this.attrsArr = [
        {
          val: ""
        },
        {
          val: ""
        }
      ];
    },
    checkData() {
      //先后顺序用Promise
      return new Promise((resolve, reject) => {
        if (this.user.specsname == "") {
          errorAlert("规格名称不能为空");
          return;
        }
        if (this.attrsArr.some(item => item.val == "")) {
          errorAlert("规格属性不能为空");
          return;
        }
        //set去重
        if (
          new Set(this.attrsArr.map(item => item.val)).size <
          this.attrsArr.length
        ) {
          errorAlert("规格属性不能重复");
          return;
        }
        resolve();
      });
    },
    //点了添加
    add() {
      this.checkData().then(() => {
        //处理attrs
        this.user.attrs = this.attrsArr.map(item => item.val).join(",");

        reqspecsadd(this.user).then(res => {
          if (res.data.code == 200) {
            //弹成功
            successAlert(res.data.msg);
            //弹框清空
            this.empty();
            //弹框消失
            this.info.isshow = false;
            //重新请求列表
            this.reqArr();
          }
        });
      });
    },
    //上级菜单发生了改变
    changepid() {
      //如果上级菜单是顶级菜单，那么类型就是目录。否则就是菜单，看pid
      if (this.user.pid == 0) {
        this.user.type = 1;
      } else {
        this.user.type = 2;
      }
    },
    //获取一条数据
    getOne(id) {
      reqspecsinfo({ id: id }).then(res => {
        if (res.data.code == 200) {
          this.user = res.data.list[0];
          this.attrsArr = this.user.attrs.map(item => ({ val: item }));
        }
      });
    },
    // 修改
    update() {
      this.checkData().then(() => {
        this.user.attrs = this.attrsArr.map(item => item.val).join(",");
        reqspecsedit(this.user).then(res => {
          if (res.data.code == 200) {
            //弹成功
            successAlert(res.data.msg);
            //弹框清空
            this.empty();
            //弹框消失
            this.info.isshow = false;
            //重新请求列表
            this.reqArr();
          }
        });
      });
    }
  }
};
</script>
<style scoped lang="less">
@import "../../../less/index.less";
.inputAndBtn {
  display: flex;
  .el-button {
    width: auto;
  }
  .el-input {
    flex: 1;
  }
}
</style>